package com.esun.service.apiImpl;

import com.esun.api.interfaces.IOperationLogApi;
import com.esun.api.request.OperationLogRequestDto;


import com.esun.api.response.BaseQueryResponseDto;
import com.esun.api.response.OperationLogResponseDto;

import com.esun.common.enums.Message;
import com.esun.common.response.RestResponse;
import com.esun.common.utils.MessageUtils;
import com.esun.service.service.OperationLogService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Service("OperationLogApi")
public class OperationLogApiImpl implements IOperationLogApi {
    @Resource
    OperationLogService operationLogService;
    @Override
    public RestResponse getOperationLogPostByPage(OperationLogRequestDto requestDto) {
        PageInfo<OperationLogResponseDto> pageInfo = operationLogService.getOperationLogByPage(requestDto);
        String message = MessageUtils.getInfoByCode(Message.QUERY_SUCCESS.getCode());
        String code = Message.QUERY_SUCCESS.getCode();
        BaseQueryResponseDto baseQueryResponseDto = new BaseQueryResponseDto();
        baseQueryResponseDto.setList(pageInfo.getList());
        baseQueryResponseDto.setCount(pageInfo.getTotal());
        return new RestResponse(code, message, baseQueryResponseDto);
    }

    @Override
    public RestResponse getLogErrorTotal() {
        OperationLogResponseDto logErrorTotal = operationLogService.getLogErrorTotal();
        String message = MessageUtils.getInfoByCode(Message.QUERY_SUCCESS.getCode());
        String code = Message.QUERY_SUCCESS.getCode();
        BaseQueryResponseDto baseQueryResponseDto = new BaseQueryResponseDto();
        baseQueryResponseDto.setCount(Long.valueOf(logErrorTotal.getTotal()));
        return new RestResponse(code, message, baseQueryResponseDto);
    }

    @Override
    public RestResponse addOperationLog(List<OperationLogRequestDto> requestDtoList) {
        List<OperationLogResponseDto> resultResponseDtoList = operationLogService.addOperationLog(requestDtoList);
        String code = Message.QUERY_SUCCESS.getCode();
        for (OperationLogResponseDto responseDto : resultResponseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                code = Message.QUERY_ERROR.getCode();
                break;
            }
        }
        String message = Message.getInfoByCode(code);
        return new RestResponse(code, message, resultResponseDtoList);
    }

    @Override
    public RestResponse updateOperationLog(List<OperationLogRequestDto> requestDtoList) {
        List<OperationLogResponseDto> resultResponseDtoList = operationLogService.updateOperationLog(requestDtoList);
        String code = Message.QUERY_SUCCESS.getCode();
        for (OperationLogResponseDto responseDto : resultResponseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                code = Message.QUERY_ERROR.getCode();
                break;
            }
        }
        String message = Message.getInfoByCode(code);
        return new RestResponse(code, message, resultResponseDtoList);
    }

    @Override
    public RestResponse deleteOperationLog(List<OperationLogRequestDto> requestDtoList) {
        List<OperationLogResponseDto> resultResponseDtoList = operationLogService.deleteOperationLog(requestDtoList);
        String code = Message.QUERY_SUCCESS.getCode();
        for (OperationLogResponseDto responseDto : resultResponseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                code = Message.QUERY_ERROR.getCode();
                break;
            }
        }
        String message = Message.getInfoByCode(code);
        return new RestResponse(code, message, resultResponseDtoList);
    }

    @Override
    public RestResponse importOperationLog(MultipartFile file) {
        //初步处理Excel文件
        Workbook workbook = null;
        try {
            InputStream inputStream = file.getInputStream();
            workbook = WorkbookFactory.create(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<OperationLogResponseDto> responseDtoList = operationLogService.importOperationLog(workbook);
        String code = Message.QUERY_SUCCESS.getCode();
        for (OperationLogResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                code = Message.QUERY_ERROR.getCode();
                break;
            }
        }
        String message = Message.getInfoByCode(code);
        return new RestResponse(code, message, responseDtoList);
    }

    @Override
    public void exportOperationLog(OperationLogRequestDto requestDto) {
        operationLogService.exportOperationLog(requestDto);
    }

    @Override
    public void downloadTemplate(String path) {
        operationLogService.downloadTemplate(path);
    }
}
